package controller;

import dao.JobDao;
import dao.SocketLogDao;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/ws/algorithm/process")
public class AlgorithmProcess  extends HttpServlet {

    private SocketLogDao socketLogDao = new SocketLogDao();
    private JobDao historyJob = new JobDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String subTaskHash = request.getParameter("subTaskHash");
        boolean isSuccess = true;
        String message;
        if(subTaskHash!=null) {
            // 根据子任务hash查询作业id
            List<Object> jobIdList = historyJob.getJobIdByTaskHash(subTaskHash);
            /*if (jobIdList.size() != 0) {
                String jobId = (String) jobIdList.get(0);

                // 根据作业id查询算法进度
            //    String algorithmProcess = socketLogDao.getAlgorithmProcessByJobId(jobId);
            String algorithmProcess = socketLogDao.getAlgorithmProcessByJobId(subTaskHash);
                if(algorithmProcess.equals("")){
                    message = "0";
                }else {
                    message = algorithmProcess;
                }
            }else {
                isSuccess = false;
                message="获取状态失败, 该任务不存在!";
            }*/
            String algorithmProcess = socketLogDao.getAlgorithmProcessBySubTaskHash(subTaskHash);
            if(algorithmProcess.equals("")){
                message = "0";
            }else {
                message = algorithmProcess;
            }
        }else{
            isSuccess = false;
            message="获取状态失败, 请正确输入参数!";
        }
        JSONObject resultJson = new JSONObject();
        resultJson.put("result", isSuccess);
        resultJson.put("message", message);
        response.getWriter().write(resultJson.toString());

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

}
